using System;
using System.Xml.Serialization;

namespace SharpMud.MudLib.SAMPLE.Environment
{
	/// <summary>
	/// Represents an area, as to what System.Type it is composed of, and what file
	/// contains its instance data
	/// </summary>
	[XmlRoot("Area")]
	public class AreaListItem
	{

		public AreaListItem()
		{
		}

		/// <summary>
		/// The filename of the file that contains the instance data for this area
		/// </summary>
		[XmlElement("FileName")]
		public string									FileName;

		/// <summary>
		/// The loose reference to the System.Type that implements this area
		/// </summary>
		[XmlElement("AreaType")]
		public SharpMud.DynamicTyping.AssembliedTypeLocation	AreaType;

		/// <summary>
		/// This loads the area and its type into memory
		/// </summary>
		public Environment.Area LoadArea(string areasPath)
		{
			if(AreaType==null)
				throw new InvalidOperationException("Cannot load area. The AreaListItem has not been properly initialized.");
			System.Type areaType = AreaType.AcquireType();
			if(areaType==null)
				throw new System.Exception("Unable to acquire Area Type!");
			System.Xml.Serialization.XmlSerializer xs = new XmlSerializer(areaType);
			System.IO.FileStream fs = new System.IO.FileStream(
				MudLibRoot.GetSubfileIntelligently(areasPath,FileName),System.IO.FileMode.Open,System.IO.FileAccess.Read);
			Environment.Area a = (Area)xs.Deserialize(fs);
			fs.Close();
			return a;
		}
	}
}
